<template>
  <div>
    <el-dialog :title="title" :visible="visible" @close="handleClose" width="800px">
      <el-form :model="form" label-position="right" :rules="rules" ref="formRef">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="剂型名称" :label-width="formLabelWidth" prop="formulationName">
              <el-input v-model="form.formulationName" placeholder="请输入剂型名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="剂型编号" :label-width="formLabelWidth" prop="formulationCode">
              <el-input v-model="form.formulationCode" placeholder="请输入剂型编号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="图标选择" :label-width="formLabelWidth" prop="picClassId">
              <el-select clearable v-model="form.picClassId" style="width: 100%;" placeholder="请选择图标">
                <el-option v-for="(item, index) in getPicData" :key="index" :value="item.picClassId"
                           :label="item.picClassName">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="启用状态" :label-width="formLabelWidth" prop="enableState">
              <el-switch v-model="form.enableState" active-text="启用" inactive-text="停用" :active-value="'1'"
                         :inactive-value="'0'">
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  informationAddOperation,
} from "@/api/index/warehouse";
import {
  findAllBasicPicInfoList,
  stoAreaEdit,//修改库区
} from '@/api/index/yxl.js'

export default {
  data() {
    return {
      form: {
        formulationCode: '',
        formulationName: '',
        stoId: '',
        stoAreaId: '',
        enableState: 0
      },
      getPicData: [],
      typeData: [],
      title: '',
      formLabelWidth: '80px',
      rules: {
        formulationName: [{
          required: true,
          message: '剂型名称不能为空',
          trigger: 'blur'
        },],
        formulationCode: [{
          required: true,
          message: '剂型编码不能为空',
          trigger: 'blur'
        }],
        enableState: [{
          required: true,
          message: '请选择启用状态',
          trigger: 'change'
        }],
        picClassId: [{
          required: true,
          message: '请选择图标',
          trigger: 'blur'
        }],
      }
    }
  },
  props: ['visible', 'stoId', 'stoAreaId'],
  created() {
    this.findAllBasicPicInfoList();
  },
  methods: {
    //获取图标
    findAllBasicPicInfoList() {
      findAllBasicPicInfoList().then(res => {
        this.getPicData = res.data;
      })
    },
    openDialog(type, val) {
      this.title = type === 'edit' ? '编辑剂型' : '新增剂型';
      // 根据类型区分新增和编辑逻辑
      if (type === 'edit' && val) {
        this.form = {
          ...val
        };
        this.form.picClassId = !this.form.picClassId ? '': Number(this.form.picClassId);
      } else {
        this.form = {
          formulationCode: '',
          formulationName: '',
          enableState: 0
        };
      }
    },
    // 提取重置表单的逻辑
    handleClose() {
      this.$nextTick(() => {
        this.$refs.formRef.resetFields();
      })
      // 通过事件通知父组件更新visible值，触发update:visible事件
      this.$emit('update:visible', false);
    },
    //提交表单
    submit() {
      this.form.stoAreaId = this.stoAreaId;
      this.form.stoId = this.stoId;
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          const apiMethod = this.title === '新增剂型' ? informationAddOperation : stoAreaEdit;
          // 调用相应的 API 方法
          this.form.picClassId = String(this.form.picClassId);
          apiMethod(this.form).then(res => {
            this.$emit('addSuccess'); // 通知父组件添加成功
            this.$message({
              message: res.msg, // 显示成功消息
              type: 'success',
            });
            // 提交成功后可以选择清空表单
            this.handleClose();
          }).catch(err => {
            this.$message({
              message: err?.msg || '操作失败', // 捕获错误信息
              type: 'error',
            });
          });
        } else {
          // 校验失败
          return false;
        }
      });
    }
  }
}
</script>

<style></style>
